package com.bst.datax.executor.service.jobhandler.streamload;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class DorisDataConverter {

    public String toCsv(List<Map<String, Object>> data) {
        return data.stream()
                .map(row -> row.values().stream()
                        .map(Object::toString)
                        .collect(Collectors.joining(",")))
                .collect(Collectors.joining("\n"));
    }

    public String toJson(List<Map<String, Object>> data) {
        try {
            return new ObjectMapper().writeValueAsString(data);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON conversion failed", e);
        }
    }
}
